package com.controller;

import java.time.Duration;

import java.time.LocalDateTime;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.socket.SocketServer;
import com.util.FrameDto;
import com.util.FrameUtil;
import com.util.Result;
import com.util.SerialPortManager;

@CrossOrigin
@RestController
public class PortController {
	
	@Autowired
	private SocketServer socketServer;
	
	
	@RequestMapping(value = "/checkStatus",method = RequestMethod.POST,produces = "application/json")
	public Result<ArrayList<String>> checkStatus() {
		Result<ArrayList<String>> result = new Result<>();
		ArrayList<String> ports = SerialPortManager.findPorts();
		result.setData(ports);
		if(SerialPortManager.openPort==null) {
			result.setMsg("未检查到连接");
		}else {
			result.setMsg("已经连接到"+SerialPortManager.openPort);
			
		}
		result.setCode(200);
		return result;
	}
	
	@RequestMapping(value = "setPort",produces = "application/json")
	public Result<String> setPort(String port) {
		Result<String> result = socketServer.startLink(port);
		if(result.getCode()==200||result.getCode()==600) {
			String dataString="7E01010000007F";
			try {
				byte[] toBytes = FrameUtil.hexStringToBytes(dataString);
				boolean sendFlag = SerialPortManager.sendToPort(SerialPortManager.openPort, toBytes);
				if(!sendFlag) {
					result.setCode(500);
					result.setMsg("读取失败");
					System.out.println("数据读取失败");
					return result;
				}
				LocalDateTime begin = LocalDateTime.now();
				System.out.println("开始等待设备返回数据.....");
				while (SerialPortManager.readData==null) {
					//等待数据读取
					LocalDateTime now = LocalDateTime.now();
					Duration duration = Duration.between(begin, now);
					long millis = duration.toMillis();
					if(millis>=5000) {//5秒断开
						System.out.println("5秒断开超时断开");
						break;
					}
				}
				if(SerialPortManager.readData==null) {
					result.setCode(500);
					result.setMsg("读取失败");
					System.out.println("数据读取失败");
				}else {
					System.out.println("成功获取到数据==>"+FrameUtil.bytesToHexString(SerialPortManager.readData));
					result.setMsg("已经连接到"+SerialPortManager.openPort+",返回数据为"+FrameUtil.bytesToHexString(SerialPortManager.readData));
					SerialPortManager.readData=null;
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.setCode(500);
				result.setMsg("系统出错");
				System.out.println("数据读取失败");
			}
			
		}
		return result;
	}
	
	@RequestMapping(value = "reCover",produces = "application/json",method = RequestMethod.POST)
	public Result<String> reCover(){
		Result<String> result = new Result<>();
		String dataString="order_0b";
		try {
			FrameDto frameDto = FrameUtil.createData(dataString);
			byte[] postData = FrameUtil.toPostData(frameDto);
			System.out.println("这里发的"+FrameUtil.bytesToHexString(postData));
			boolean sendToPort = SerialPortManager.sendToPort(SerialPortManager.openPort, postData);
			if(sendToPort) {
				result.setCode(200);
				result.setMsg("发送数据");
			}
			
		} catch (Exception e) {
			result.setCode(500);
			result.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
}
